package pl.polsl.mmm.projectcomm.objects.state;

import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import pl.polsl.mmm.projectcomm.Constants;
import pl.polsl.mmm.projectcomm.data.vec3;
import pl.polsl.mmm.projectcomm.data.vec4;

/**
 * Created by michal on 29.12.14.
 */
public class BobState implements Serializable {
    public vec3 location;
    public vec4 rotation;
    public ArrayList<MD5ActionState> md5action;
    public Constants.Model_state modelState;
    public float speedRate;
    public vec3 touchedPoint;
    public int gesture;

    public static BobState createFromByteArray(byte[] byteArray) {
        ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
        ObjectInput in = null;
        BobState bobState = null;

        try {
            in = new ObjectInputStream(bis);
            bobState = (BobState)in.readObject();

        } catch (ClassNotFoundException e) {
//            Log.e("Exception: ", e.getMessage());
        }
        catch (StreamCorruptedException e) {
//            Log.e("Exception: ", e.getMessage());
        }
        catch (IOException e) {}
        finally {
            try {
                bis.close();
            } catch (IOException ex) {}
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {}
        }

        return bobState;
    }

    public byte[] getByteArray() {
        byte[] byteArray = {};

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(this);
            byteArray = bos.toByteArray();
        } catch (IOException e) {
            Log.e("Exception", e.getMessage());
        }

        finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {}
            try {
                bos.close();
            } catch (IOException ex) {}
        }

        return byteArray;
    }
}
